package com.umer.pinban.adapter.customer;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.common.ui.recycleview.adapter.RecyclerviewBasicAdapter;
import com.common.ui.recycleview.base.ViewHolder;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.umer.pinban.R;
import com.umer.pinban.bean.customer.CustomerBean;
import java.util.List;

public class SelectCustomerAdapter extends RecyclerviewBasicAdapter<CustomerBean> {

    private OptListener optListener;
    private DataStateListener dataStateListener;

    public SelectCustomerAdapter(Context context, List<CustomerBean> datas, int layoutId,
                                 OptListener optListener,DataStateListener dataStateListener) {
        super(context, datas, layoutId,dataStateListener);
        this.optListener = optListener;
        this.dataStateListener = dataStateListener;
    }

    @Override
    public void convert(ViewHolder holder, final CustomerBean customerBean, final int position) {
        LinearLayout linear = holder.getView(R.id.linear_select);
        TextView tv1 = holder.getView(R.id.tv);
        TextView tv_phone = holder.getView(R.id.tv_phone);
        TextView tv_head = holder.getView(R.id.tv_head);
        ImageView iv = holder.getView(R.id.iv_select);
        /*SimpleDraweeView simpleDraweeView = holder.getView(R.id.simpleDraweeView);
        String path = bean.getPath();
        if(!TextUtils.isEmpty(path)){
            Uri uri = Uri.parse(Constants.IP_PORT_DEFAULT_PICTURE+path);
            simpleDraweeView.setImageURI(uri);
        }else {
            Uri uri = Uri.parse("");
            simpleDraweeView.setImageURI(uri);
        }*/

        String name = customerBean.getName();
        if(name!=null && !name.equals("") && !name.equals(" ") && !name.equals("null")){
            tv1.setText(name);
            String firstName = name.substring(0,1);
            tv_head.setText(firstName);
            if(!isChinese(firstName)){
                tv_head.setText(firstName.toUpperCase());
            }
        }else {
            tv1.setText("");
        }

        String phone = customerBean.getPhone();
        if(phone!=null && !phone.equals("") && !phone.equals(" ") && !phone.equals("null")){
            tv_phone.setText(phone);
        }else {
            tv_phone.setText("");
        }

        boolean select = customerBean.isSelect();
        if(select){
            iv.setBackgroundResource(R.drawable.select_yes);
        }else {
            iv.setBackgroundResource(R.drawable.select_no);
        }

        linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                optListener.onOptClick(v,position);
            }
        });
    }

    // 判断一个字符是否是中文
    public boolean isChinese(String  str) {
        for (int i = 0; i < str.length(); i++) {
            String bb = str.substring(i, i + 1);
            // 生成一个Pattern,同时编译一个正则表达式,其中的u4E00("一"的unicode编码)-\u9FA5("龥"的unicode编码)
            boolean cc = java.util.regex.Pattern.matches("[\u4E00-\u9FA5]", bb);
            if (cc == false) {
                return cc;
            }
        }
        return true;
    }
}